package com.woniuxy.conf;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 曾令忠
 * @Description:
 * @Date: 2020/7/7 0007-上午 11:10
 */
@Configuration
public class RabbitConf2 {
    //延时交换机
    @Bean
    public DirectExchange delayExchange(){
        return new DirectExchange("delay_exchange");
    }
    //死信交换机
    @Bean
    public DirectExchange deadDelayExchange(){
        return new DirectExchange("delay_dead_exchange");
    }
    //延时队列
    @Bean
    public Queue delayQueue(){
        Map<String,Object> map = new HashMap<>();
        //绑定死信交换机
        map.put("x-dead-letter-exchange","delay_dead_exchange");
        //绑定当前队列死信的路由
        map.put("x-dead-letter-routing-key","dead-delay-key");
        //设置整个消息队列的过期时间,整个消息队列的消息的过期时间都是3秒，时间到后没被消费自动变成死信
        map.put("x-message-ttl",3000);
        //创建队列
        return new Queue("delayQueue",true,false,false,map);
    }
    //死信队列
    @Bean
    public Queue deadDelayLetterQueue(){
        return new Queue("dead_delay_Queue");
    }

    //绑定延时队列和延时交换机
    @Bean
    public Binding bindingDelayQueueAndExchange(Queue delayQueue,DirectExchange delayExchange){
        return BindingBuilder.bind(delayQueue).to(delayExchange).with("delay");
    }
    //绑定死信队列和死信交换机
    @Bean
    public Binding bindingDeadDelayQueueAndExchange(Queue deadDelayLetterQueue,DirectExchange deadDelayExchange){
        return BindingBuilder.bind(deadDelayLetterQueue).to(deadDelayExchange).with("dead-delay-key");
    }
//    @Bean
//    public RabbitListenerContainerFactory<?> rabbitListenerContainerFactory(ConnectionFactory connectionFactory){
//        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
//        factory.setConnectionFactory(connectionFactory);
//        factory.setMessageConverter(new Jackson2JsonMessageConverter());
//        factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
//        return factory;
//    }

}
